#include "device_oled.h"
#include "spi_soft.h"

static spi_soft_node_t device_spi;

static void spi_delay(uint32_t ms)
{
    uint32_t count = (SystemCoreClock / 1000000) / 2;
    while (count-- > 0);
}

static void spi_mosi(SPI_SOFT_U8 status)
{
    status == 0 ? gpio_bit_reset(GPIOA,GPIO_PIN_7) : gpio_bit_set(GPIOA,GPIO_PIN_7);
}

static void spi_sck(SPI_SOFT_U8 status)
{
    status == 0 ? gpio_bit_reset(GPIOA,GPIO_PIN_5) : gpio_bit_set(GPIOA,GPIO_PIN_5);
}

static void spi_ss(SPI_SOFT_U8 status)
{
    status == 0 ? gpio_bit_reset(GPIOA,GPIO_PIN_6) : gpio_bit_set(GPIOA,GPIO_PIN_6);
}

void device_oled_init(void)
{
    device_spi.cpha_mode = 1;
    device_spi.cpol_mode = 0;
    device_spi.delay_func =  vbsp_spi_delay;
    device_spi.mosi_func =  spi_mosi;
    device_spi.miso_func =  NULL;
    device_spi.sck_func =   spi_sck;
    device_spi.ss_func =    spi_ss;
    spi_soft_init(&device_spi);
    spi_soft_ss_set(&device_spi, 0);
    spi_soft_byte_CTRC(&device_spi, 0x66);
    spi_soft_ss_set(&device_spi, 1);
    spi_soft_ss_set(&device_spi, 0);
    spi_soft_byte_CTRC(&device_spi, 0x55);
    spi_soft_ss_set(&device_spi, 1);
    spi_soft_ss_set(&device_spi, 0);
    spi_soft_byte_CTRC(&device_spi, 0x78);
    spi_soft_ss_set(&device_spi, 1);
}